'use strict';

const Controller = require('egg').Controller;

// Recycle回收合约的
class RecycleController extends Controller {
  // 查询Recycle的双重签名是否使用
  async signHashStatusRecycle() {
    const { app, ctx, service } = this;
    const messageHash = ctx.request.body.messageHash;
    const res = await service.recycle.signHashStatusRecycle2(messageHash);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 查询Recycle的orderId是否使用
  async orderIdMappingRecycle() {
    const { app, ctx, service } = this;
    const orderId = ctx.request.body.orderId;
    const res = await service.recycle.orderIdMappingRecycle2(orderId);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 扫描质押事件
  async scanDepositRecycle() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Deposit';
    const res = await service.recycle.scanEventRecycle2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        orderId: res[i].result.orderId,
        time: res[i].result.time,
        typology: res[i].result.typology,
        etmValue: res[i].result.etmValue,
        userAddress0x: res[i].result.owner,
        userAddress: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 用户提取收益加本金的二次签名;
  async withdrawRecycleSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);

    // 验证的数据有: 函数名, 订单号, 用户地址, 提取的ETM数量, 随机数;
    const orderId = ctx.request.body.orderId;
    const userAddress = ctx.request.body.userAddress;
    const etmValue = ctx.request.body.etmValue;
    const nonce = ctx.request.body.nonce;

    // 验证地址
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // 金额必须大于0, 不能有小数
    if(ctx.helper.lte(etmValue, '0') || ctx.helper.isDot(etmValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(orderId, '0') || ctx.helper.isDot(orderId)) {
      ctx.body = { code: 1001, msg: 'orderId不能小于0, 不能是小数', data: {} };
      return;
    }


    const userAddress0x = await service.eth.addressToHex2(userAddress);
    // 开始签名
    const res = await service.recycle.withdrawRecycleSecondSignature2(
      orderId, userAddress0x, etmValue, nonce
    );
    const data = {
      // 默认地址
      userAddress: userAddress,
      // 转换之后的地址
      userAddress0x: userAddress0x,
      // 其它值
      orderId: orderId,
      etmValue: etmValue,
      nonce: nonce,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 扫描用户提取收益加本金事件
  async scanWithdrawRecycle() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Withdraw';
    const res = await service.recycle.scanEventRecycle2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        orderId: res[i].result.orderId,
        etmValue: res[i].result.etmValue,
        nonce: res[i].result.nonce,
        userAddress0x: res[i].result.owner,
        userAddress: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 回收NFT卡牌二次签名;
  async recycleCardSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);

    // 验证的数据有: 函数名, nft卡牌id, 用户地址, 提取的ETM数量, 随机数;
    const nftId = ctx.request.body.nftId;
    const userAddress = ctx.request.body.userAddress;
    const etmValue = ctx.request.body.etmValue;
    const nonce = ctx.request.body.nonce;

    // 验证地址
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // 金额必须大于0, 不能有小数
    if(ctx.helper.lte(etmValue, '0') || ctx.helper.isDot(etmValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nftId, '0') || ctx.helper.isDot(nftId)) {
      ctx.body = { code: 1001, msg: 'orderId不能小于0, 不能是小数', data: {} };
      return;
    }


    const userAddress0x = await service.eth.addressToHex2(userAddress);
    // 开始签名
    const res = await service.recycle.recycleCardSecondSignature2(
      nftId, userAddress0x, etmValue, nonce
    );
    const data = {
      // 默认地址
      userAddress: userAddress,
      // 转换之后的地址
      userAddress0x: userAddress0x,
      // 其它值
      nftId: nftId,
      etmValue: etmValue,
      nonce: nonce,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 用户领取挖矿收益的二次签名;
  async userRedRecycleSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);

    // 验证的数据有: 函数名, 用户地址, 挖矿的ETM数量, 随机数;
    const userAddress = ctx.request.body.userAddress;
    const etmValue = ctx.request.body.etmValue;
    const nonce = ctx.request.body.nonce;

    // 验证地址
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // 金额必须大于0, 不能有小数
    if(ctx.helper.lte(etmValue, '0') || ctx.helper.isDot(etmValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }

    const userAddress0x = await service.eth.addressToHex2(userAddress);
    // 开始签名
    const res = await service.recycle.userRedRecycleSecondSignature2(
      userAddress0x, etmValue, nonce
    );
    const data = {
      // 默认地址
      userAddress: userAddress,
      userAddress0x: userAddress0x,
      // 其它值
      etmValue: etmValue,
      nonce: nonce,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 扫描领取挖矿收益事件;
  async scanUserRedRecycle() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'UserRed';
    const res = await service.recycle.scanEventRecycle2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        etmValue: res[i].result.etmValue,
        nonce: res[i].result.nonce,
        userAddress0x: res[i].result.owner,
        userAddress: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 扫描用户回收NFT卡牌事件
  async scanRecycleCard() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'RecycleNft';
    const res = await service.recycle.scanEventRecycle2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        nftId: res[i].result.nftId,
        etmValue: res[i].result.etmValue,
        nonce: res[i].result.nonce,
        userAddress0x: res[i].result.owner,
        userAddress: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }



}

module.exports = RecycleController;
